﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace G.Controls
{
    [TemplatePart(Name = ContentBorderName, Type = typeof(Border))]
    [TemplateVisualState(GroupName = "NormalGroup", Name = "Open")]
    [TemplateVisualState(GroupName = "NormalGroup", Name = "Close")]
    public class GMessageBox : ChildWindow
    {
        private const string ContentBorderName = "ContentBorder";
        public static void Show(string title, string message)
        {
            GMessageBox g = new GMessageBox();
            g.Title = title;
            TextBlock t = new TextBlock() { Text = message, FontSize = 20, TextWrapping = TextWrapping.Wrap };
            t.SetBinding(TextBlock.ForegroundProperty, BindingHelp.Create("Foreground", g));
            g.Content = t;
            g.CommandContent = new Button() { Content = "确定" };
            g.IsOpen = true;
        }


        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(GMessageBox), new PropertyMetadata("Title"));

        public string Title
        {
            get
            {
                return this.GetValue(TitleProperty) as string;
            }
            set
            {
                this.SetValue(TitleProperty, value);
            }
        }

        public static readonly DependencyProperty CommandContentProperty = DependencyProperty.Register("CommandContent", typeof(object), typeof(GMessageBox), null);
        /// <summary>
        /// 消息框底部命令内容
        /// </summary>
        public object CommandContent
        {
            get
            {
                return this.GetValue(CommandContentProperty);
            }
            set
            {
                this.SetValue(CommandContentProperty, value);
            }
        }
        public GMessageBox()
        {
            this.DefaultStyleKey = typeof(GMessageBox);
        }
        bool isApply;
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            isApply = true;
        }
        protected override void OnOpened()
        {
            base.OnOpened();
            Microsoft.Devices.VibrateController vc = Microsoft.Devices.VibrateController.Default;
            vc.Start(TimeSpan.FromSeconds(0.2));
            if (!isApply)
                this.ApplyTemplate();
            VisualStateManager.GoToState(this, "Open", false);
        }
        protected override void OnClosed()
        {
            base.OnClosed();
            VisualStateManager.GoToState(this, "Close", false);
        }
    }
}
